//
//  RequestManager.m
//  LoverGo
//
//  Created by YeXiao on 14-2-16.
//  Copyright (c) 2014年 yexiao. All rights reserved.
//

#import "DataCenter.h"
#import "UserInfo.h"
#import "MJExtension.h"
#import "RequestManager.h"
#import "Toast+UIView.h"


RequestManager *singletonM = nil;

@implementation RequestManager


+ (RequestManager *)getSingleton {
    if(!singletonM) {
        singletonM = [[RequestManager alloc] init];
        singletonM->svhttpClient = [SVHTTPClient sharedClient];
        [singletonM->svhttpClient setBasePath:baseUrl];
        singletonM->standard = [NSUserDefaults standardUserDefaults];
        singletonM->showTimer = nil;
    }
    return singletonM;
}


-(void)stopLoading
{
    if (showTimer) {
        [showTimer invalidate];
        showTimer = nil;
    }
}
-(void)sendRequest:(NSString*)sPath para:(NSDictionary*)p callBack:(RequestManagerHandler)cb
{
    [[SVHTTPClient sharedClient] POST:sPath
                          parameters:p
                          completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
                              if (error) {
                                  [[[[UIApplication sharedApplication] delegate] window] makeToast:@"网络接连失败"
                                                    duration:1.0
                                                    position:@"center"];
                                  return ;
                              }
                              NSError *jsonError = nil;
                              id jsonObject = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&jsonError];
                              if (jsonError) {
                                  [[[[UIApplication sharedApplication] delegate] window] makeToast:@"数据解析异常"
                                                                                          duration:1.0
                                                                                          position:@"center"];
                                  return;
                              }
                              if(cb)
                              {
                                  cb(jsonObject);
                              }
                          }];
}
-(void)sendRequestAboutUser:(NSString*)sPath para:(NSDictionary*)p
{
    [[RequestManager getSingleton] sendRequest:sPath para:p callBack:^(id jsonObj) {
        NSArray* arr= [UserInfo objectArrayWithKeyValuesArray:[jsonObj objectForKey:@"data"]];
        if (arr && arr.count > 0) {
            UserInfo *uInfo = [arr objectAtIndex:0];
            if (uInfo) {
                [[DataCenter getSingleton] setUserInfo:uInfo];
            }
            [[NSNotificationCenter defaultCenter]postNotificationName:sPath object:jsonObj];
        }else
        {
            [[NSNotificationCenter defaultCenter]postNotificationName:LoginErrorNof object:jsonObj];
        }
    }];
}

-(void)sendRequest:(NSString*)sPath para:(NSDictionary*)p notJsoncallBack:(RequestManagerHandler)cb
{
    [[SVHTTPClient sharedClient] POST:sPath
                           parameters:p
                           completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
                               if (error) {
                                   [[[[UIApplication sharedApplication] delegate] window] makeToast:@"网络接连失败"
                                                                                           duration:1.0
                                                                                           position:@"center"];
                                   return ;
                               }
                               if(cb)
                               {
                                   cb(response);
                               }
                           }];
}
@end
